package ttc.mBackup.BackupAndRestoreSMS;

import java.util.ArrayList;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonParseException;

public class SMSList extends Activity {
	
	private SMSQuery smsQuery;
	private SMSClass smsClass;
	private ArrayList<SMSClass> arraySMSClass;
	private String m_id;
	private String m_body;
	private String m_address;
	private ArrayList<String> m_ArrayIdSMS;
	private ArrayList<SMSClassCustomViewGroup> arraySMSClassCustomViewGroup;
	SMSListArrayAdapter smsArrayAdapter;
	private ListView lstView;
	private Button btnBackup;
	private Cursor cursorsmsquery;
	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		
		smsQuery=new SMSQuery(getApplicationContext());
		smsClass= new SMSClass();
		setContentView(R.layout.layout_list_sms);
		btnBackup=(Button)findViewById(R.id.btnBackup);
		cursorsmsquery=smsQuery.getAllSMS();
		getSMSForDisplay();
		btnBackup_Click();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu)
	{
		MenuInflater inflater=getMenuInflater();
		inflater.inflate(R.menu.menu_list_sms, menu);
		return true;
	}
	/* Function for click menu
	 * (non-Javadoc)
	 * @see android.app.Activity#onMenuItemSelected(int, android.view.MenuItem)
	 */
	@Override 
	public boolean onMenuItemSelected(int featureId,MenuItem item)
	{
		int id=(item.getItemId());
		Log.d("Menu",Integer.toString(item.getItemId()));
		switch (id)
		{
			case R.id.menu_AllSMS:
				Log.d("menu_AllSMS",Integer.toString(item.getItemId()));
				cursorsmsquery=smsQuery.getAllSMS();
				getSMSForDisplay();
				return true;
			case R.id.menu_Inbox:
				Log.d("menu_Inbox",Integer.toString(item.getItemId()));
				cursorsmsquery=smsQuery.getInboxSMS();
				getSMSForDisplay();
				return true;
			case R.id.menu_Sent:
				Log.d("menu_Sent",Integer.toString(item.getItemId()));
				cursorsmsquery=smsQuery.getOutboxSMS();
				getSMSForDisplay();
				return true;
			case R.id.menu_Draft:
				Log.d("menu_Draft",Integer.toString(item.getItemId()));
				getSMSForDisplay();
				return true;
							
		}
		//Log.d("Menu",Integer.toString(item.getItemId()));
		return super.onMenuItemSelected(featureId, item);
	}

	public void btnBackup_Click()
	{
		btnBackup.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				m_ArrayIdSMS= new ArrayList<String>(smsArrayAdapter.getCount());
				Log.i("ArrayAdapter",Integer.toString(smsArrayAdapter.getCount()));
				m_ArrayIdSMS=smsArrayAdapter.arrayIdSMS;
				//Log.i("ArrayAdapter",Integer.toString(smsArrayAdapter.arrayIdSMS.size()));
				
				if(smsArrayAdapter.arrayIdSMS!=null)
				{
					if(smsArrayAdapter.arrayIdSMS.isEmpty())
					{
						Log.i("tag","m_ArrayIdSMSEmpty");
					}
					else
					{	Log.i("idsms",smsArrayAdapter.arrayIdSMS.get(0).toString());
						arraySMSClass=new ArrayList<SMSClass>(smsArrayAdapter.arrayIdSMS.size());
						try
						{
							for(int i=0;i<smsArrayAdapter.arrayIdSMS.size();i++)
							{
								smsClass=getSMS(smsArrayAdapter.arrayIdSMS.get(i).toString());
								arraySMSClass.add(smsClass);
							}
							convertSMSToJSon();
						}
						catch(Exception ex)
						{
							throw new RuntimeException(ex);
						}
						Log.i("tag",Integer.toString(arraySMSClass.size()));
					}
				}	
			}
		});
	}
	
	public SMSClass getSMS(String IdSMS)
	{
		return smsQuery.getSMSById(IdSMS);
	}
	
	public void convertSMSToJSon()
	{
		StringBuilder buiderJSon= new StringBuilder();
		GsonBuilder gSonBuider= new GsonBuilder();
		Gson gSon=gSonBuider.create();
		String s;
		for(int i=0;i< arraySMSClass.size();i++)
		{
			try
			{
				//Log.i("json",s);
				buiderJSon.append(gSon.toJson(arraySMSClass.get(i)));
				Log.i("json",buiderJSon.toString());
			}
			catch(JsonParseException ex)
			{
				Toast.makeText(this, "Have Error to parse to Json", Toast.LENGTH_LONG);
			}
		}
	}
	
	/*
	 *	Name 	: Get SMS from SQL
	 *	Input Value: 
	 *	Output Value
	 *	Description: Get SMS from SQL,Draft,Inbox,Sent or all SMS
	 */
	public void getSMSForDisplay()
	{
		arraySMSClassCustomViewGroup= new ArrayList<SMSClassCustomViewGroup>();
		cursorsmsquery.moveToFirst();
		do
		{
			 m_id= cursorsmsquery.getString(cursorsmsquery.getColumnIndex("_id"));
			 m_body=cursorsmsquery.getString(cursorsmsquery.getColumnIndex("body"));
			 m_address=cursorsmsquery.getString(cursorsmsquery.getColumnIndex("address"));
			 SMSClassCustomViewGroup smsClassCustomViewGroup= new SMSClassCustomViewGroup(m_body, m_address, m_id, false);
			 arraySMSClassCustomViewGroup.add(0,smsClassCustomViewGroup);
			 
			 cursorsmsquery.moveToNext();
		}while(!cursorsmsquery.isLast());
		smsArrayAdapter= new SMSListArrayAdapter(this, R.layout.layout_list_sms	, arraySMSClassCustomViewGroup);
		
		lstView= (ListView) findViewById(R.id.listSMS);
		if(lstView.getCount()>0)
			lstView.removeAllViewsInLayout();
		lstView.setAdapter(smsArrayAdapter);
		
	}
}
